# -*- tcl -*-
# finite_automaton.test:  tests for the grammar::fa container.
#
# Copyright (c) 2004-2007 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
#
# RCS: @(#) $Id: fa_useful.test,v 1.4 2007/04/12 03:43:15 andreas_kupries Exp $

# -------------------------------------------------------------------------

test fa-useful-${setimpl}-1.0 {usefulness} {
    grammar::fa a
    catch {a useful_states x} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methoduseful_states type selfns win self"}


test fa-useful-${setimpl}-1.1 {!usefulness} {
    grammar::fa a
    catch {a unuseful_states a} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methodunuseful_states type selfns win self"}


test fa-useful-${setimpl}-1.2 {usefulness} {
    grammar::fa a
    catch {a useful} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methoduseful type selfns win self s"}


test fa-useful-${setimpl}-1.3 {usefulness} {
    grammar::fa a
    catch {a useful x} res
    a destroy
    set res
} {Illegal state "x"}


test fa-useful-${setimpl}-1.4 {usefulness} {
    grammar::fa a
    catch {a useful x y} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methoduseful type selfns win self s"}


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


test fa-useful-${setimpl}-2.0 {useful states, empty fa} {
    grammar::fa a
    set res [a useful_states]
    a destroy
    set res
} {}


test fa-useful-${setimpl}-2.1 {useful states, state addition, plain} {
    grammar::fa a
    a state add x
    set res [a useful_states]
    a destroy
    set res
} {}

test fa-useful-${setimpl}-2.2 {useful states, state addition, final} {
    grammar::fa a
    a state add x
    a final add x
    set res [a useful_states]
    a destroy
    set res
} x


test fa-useful-${setimpl}-2.3 {useful states, state addition, start} {
    grammar::fa a
    a state add x
    a start add x
    set res [a useful_states]
    a destroy
    set res
} {}


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


test fa-useful-${setimpl}-3.0 {unuseful states, empty fa} {
    grammar::fa a
    set res [a unuseful_states]
    a destroy
    set res
} {}


test fa-useful-${setimpl}-3.1 {unuseful states, state addition, plain} {
    grammar::fa a
    a state add x
    set res [a unuseful_states]
    a destroy
    set res
} x

test fa-useful-${setimpl}-3.2 {unuseful states, state addition, final} {
    grammar::fa a
    a state add x
    a final add x
    set res [a unuseful_states]
    a destroy
    set res
} {}


test fa-useful-${setimpl}-3.3 {unuseful states, state addition, start} {
    grammar::fa a
    a state add x
    a start add x
    set res [a unuseful_states]
    a destroy
    set res
} x


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


foreach {n code setup_result} {
    00 x       {{} {} x   x x {}}
    01 x-      {{} {} x   x x {}}
    02 xe      {{} {} x   x x {}}
    03 xy      {{} {} {x y}  x x y  y y     x   {x y} {x y} {}}
    04 xy-     {{} {} {x y}  x x y  y {x y} {}  {x y} {x y} {}}
    05 xye     {{} {} {x y}  x x y  y {x y} {}  {x y} {x y} {}}
    06 xyee    {{} {} {x y}  x {x y} {}  y {x y} {}  {x y} {x y} {}}
    07 xye-    {{} {} {x y}  x {x y} {}  y {x y} {}  {x y} {x y} {}}
    08 xy--    {{} {} {x y}  x {x y} {}  y {x y} {}  {x y} {x y} {}}
    09 xy-=    {{} {} {x y}  x {x y} {}  y {x y} {}  {x y} {x y} {}}
    10 xyz/ee  {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x z}   y
	{x y} {x y}   z          {x z}   {x z}   y
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    11 xyz/e-  {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x z}   y
	{x y} {x y}   z          {x z}   {x z}   y
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    12 xyz/--  {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x z}   y
	{x y} {x y}   z          {x z}   {x z}   y
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    13 xyz/-=  {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x z}   y
	{x y} {x y}   z          {x z}   {x z}   y
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    14 xyz|ee  {
	{}    {}      {x y z}    x       x       {y z}
	y     y       {x z}      z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    15 xyz|e-  {
	{}    {}      {x y z}    x       x       {y z}
	y     y       {x z}      z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    16 xyz|--  {
	{}    {}      {x y z}    x       x       {y z}
	y     y       {x z}      z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    17 xyz|-=  {
	{}    {}      {x y z}    x       x       {y z}
	y     y       {x z}      z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    18 xyz+eee {
	{}    {}      {x y z}    x       {x y z} {}
	y     {x y z} {}         z       {x y z} {}
	{x y} {x y z} {}         {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    19 xyz+ee- {
	{}    {}      {x y z}    x       {x y z} {}
	y     {x y z} {}         z       {x y z} {}
	{x y} {x y z} {}         {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    20 xyz+e-- {
	{}    {}      {x y z}    x       {x y z} {}
	y     {x y z} {}         z       {x y z} {}
	{x y} {x y z} {}         {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    21 xyz+e-= {
	{}    {}      {x y z}    x       {x y z} {}
	y     {x y z} {}         z       {x y z} {}
	{x y} {x y z} {}         {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    22 xyz+--- {
	{}    {}      {x y z}    x       {x y z} {}
	y     {x y z} {}         z       {x y z} {}
	{x y} {x y z} {}         {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    23 xyz+--= {
	{}    {}      {x y z}    x       {x y z} {}
	y     {x y z} {}         z       {x y z} {}
	{x y} {x y z} {}         {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    24 xyz+-=_ {
	{}    {}      {x y z}    x       {x y z} {}
	y     {x y z} {}         z       {x y z} {}
	{x y} {x y z} {}         {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    25 xyz&eee {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    26 xyz&ee- {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    27 xyz&e-- {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    28 xyz&e-= {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    29 xyz&--- {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    30 xyz&--= {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    31 xyz&-=_ {
	{}    {}      {x y z}    x       x       {y z}
	y     {x y}   z          z       {x y z} {}
	{x y} {x y}   z          {x z}   {x y z} {}
	{y z} {x y z} {}         {x y z} {x y z} {}
    }
    32 xyz!ee  {
	{}    {}      {x y z}  x       x       {y z}
	y     {x y}   z        z       {x y z} {}
	{x y} {x y}   z        {x z}   {x y z} {}
	{y z} {x y z} {}       {x y z} {x y z} {}
    }
    33 xyz!e-  {
	{}    {}      {x y z}  x       x       {y z}
	y     {x y}   z        z       {x y z} {}
	{x y} {x y}   z        {x z}   {x y z} {}
	{y z} {x y z} {}       {x y z} {x y z} {}
    }
    34 xyz!--  {
	{}    {}      {x y z}  x       x       {y z}
	y     {x y}   z        z       {x y z} {}
	{x y} {x y}   z        {x z}   {x y z} {}
	{y z} {x y z} {}       {x y z} {x y z} {}
    }
    35 xyz!-=  {
	{}    {}      {x y z}  x       x       {y z}
	y     {x y}   z        z       {x y z} {}
	{x y} {x y}   z        {x z}   {x y z} {}
	{y z} {x y z} {}       {x y z} {x y z} {}
    }
    36 xyz!-e  {
	{}    {}      {x y z}  x       x       {y z}
	y     {x y}   z        z       {x y z} {}
	{x y} {x y}   z        {x z}   {x y z} {}
	{y z} {x y z} {}       {x y z} {x y z} {}
    }
} {
    foreach {fset useful unuse} $setup_result {
	set key ${n}.${code}.([join $fset {}])
	set expected {}
	foreach x $useful {lappend expected 1}
	foreach x $unuse  {lappend expected 0}

	test fa-useful-${setimpl}-4.$key {useful states} {
	    grammar::fa a
	    gen $code
	    a final set $fset
	    set res [lsort [a useful_states]]
	    a destroy
	    set res
	} $useful ; # {}

	test fa-useful-${setimpl}-5.$key {!useful states} {
	    grammar::fa a
	    gen $code
	    a final set $fset
	    set res [lsort [a unuseful_states]]
	    a destroy
	    set res
	} $unuse ; # {}

	test fa-useful-${setimpl}-6.$key {usefulness testing} {
	    grammar::fa a
	    gen $code
	    a final set $fset
	    set res {}
	    foreach x $useful {lappend res [a useful $x]}
	    foreach x $unuse  {lappend res [a useful $x]}
	    a destroy
	    set res
	} $expected ; # {}
    }
}


# -------------------------------------------------------------------------
::tcltest::cleanupTests
